package com.tupi.project.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tupi.project.R;

import java.util.List;

/**
 * Created by Administrator on 2017/11/2.
 */

public class MainAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
    private List<String> mList;
    private Inf mInf;
    private LayoutInflater inflater;
    private Context mContext;
    
    public MainAdapter(Context context, List<String> data, Inf inf)
    {
        inflater = LayoutInflater.from(context);
        mContext = context;
        this.mList = data;
        mInf = inf;
    }
    
    public interface Inf
    {
        void onItemClick(String title);
    }
    
    
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        return new ViewHolder(inflater.inflate(R.layout.item_main_adapter, parent, false));
    }
    
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        if (holder instanceof ViewHolder)
        {
            ViewHolder view = (ViewHolder) holder;
            final String data = mList.get(position);
            view.text.setText(data);
            view.text.setBackgroundColor(mContext.getResources().getColor(position % 2 == 0 ? R
                    .color.base_bg : R.color.white));
            view.text.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    mInf.onItemClick(data);
                }
            });
        }
    }
    
    @Override
    public int getItemCount()
    {
        return mList.size();
    }
    
    public static class ViewHolder extends RecyclerView.ViewHolder
    {
        private TextView text;
        
        public ViewHolder(View itemView)
        {
            super(itemView);
            text = (TextView) itemView.findViewById(R.id.text);
        }
    }
    
    
}
